import axios from 'axios';
const mockDianPuList = [
    { sid: 1, name: 'Shop A' },
    { sid: 2, name: 'Shop B' }
];

const mockListingData = [
    { asin: 'A123', principal_info: [{ principal_uid: 'uid1', principal_name: 'Manager1' }] },
    { asin: 'A124', principal_info: [{ principal_uid: 'uid2', principal_name: 'Manager2' }] }
];

const mockSaleData = [
    { asin: 'A123', map_value: 1000 },
    { asin: 'A124', map_value: 2000 }
];

const mockUserInfo = [
    { principal_uid: 'uid1', mobile: '1234567890' },
    { principal_uid: 'uid2', mobile: '0987654321' }
];


// 获取店铺列表
export async function getDianPuList(appId, data) {
    // 模拟返回店铺列表数据
    return new Promise(resolve => resolve(mockDianPuList));
}

// 获取店铺下的listing
export async function getListingBySid(appId, data, sid) {
    // 模拟返回店铺下的listing数据
    return new Promise(resolve => resolve(mockListingData));
}

// 获取店铺销量
export async function getSaleBySid(appId, data, sid, date) {
    // 模拟返回店铺销量数据
    return new Promise(resolve => resolve(mockSaleData));
}

// 根据负责人找用户的电话号码
export async function getUserInfo(appId, data) {
    // 模拟返回用户信息数据
    return new Promise(resolve => resolve(mockUserInfo));
}
/**
 * API鉴权
  POST /api/auth/v1/get-token
  接口ID：216509964
  接口地址：https://app.apifox.com/link/project/2073721/apis/api-216509964
 */
  export async function getAuthToken(apiKey, apiSecret) {
    const url = "http://192.168.31.7:8000/api/auth/v1/get-token";  // API URL
    
    const headers = {
        "Content-Type": "application/json"
    };

    const body = {
        apiKey: apiKey,
        secretKey: apiSecret
    };
    const response = await axios.post(url, body, { headers: headers });  
    // 检查响应状态
    // if (response.status !== 200) {
    //     throw new Error(`Error! status: ${response.status}`);
    // }
    return response.data;  // 假设返回的响应有 'access_token' 字段
}
/**
 * 零星同步销售数据
 * @param {string} accessToken - 之前获取的 access_token.
 * @param {object} saleData - 销售数据要同步的内容.
 * @returns {Promise} - 返回请求的 Promise.
 */
export async function syncLingXingSales(accessToken, saleData) {
    try {
      const response = await axios.post(
        'http://192.168.31.7:8000/api/v1/lingxing-sale/sync',
        saleData, // 请求体，包含要同步的销售数据
        {
          headers: {
            'Authorization': `Bearer ${accessToken}`, // 将 access_token 放在 Authorization 头中
            'Content-Type': 'application/json' // 确保请求体是 JSON 格式
          }
        }
      );
      return response.data;
    } catch (error) {
      console.error('同步失败:', error.response ? error.response.data : error.message);
      throw error;
    }
  }
  